package com.gitee.wsl.prism4j.ui


import androidx.annotation.ColorInt
import androidx.annotation.ColorLong
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight


class Prism4jThemeObsidian(@param:ColorInt private val background: Long) : Prism4jThemeBase() {
    override val colorHashMap = ColorHashMap()
        .add(0xff66747B, "comment", "prolog", "doctype", "cdata")
        .add(0xffF1F2F3, "punctuation")
        .add(0xff8AC763, "property", "tag", "boolean", "number", "constant", "symbol", "deleted")
        .add(0xffE0E2E4, "selector", "attr-name", "string", "char", "builtin", "inserted")
        .add(-0x6591c6, "operator", "entity", "url")
        .add(0xffEC7600, "atrule", "attr-value", "keyword")
        .add(-0x22b598, "function", "class-name")
        .add(-0x116700, "regex", "important", "variable")

    override fun background(): Long {
        return background
    }

    override fun textColor(): Long {
        return 0xffF1F2F3
    }

    override fun applyColor(
        language: String,
        type: String,
        alias: String?,
        @ColorLong color: Long,
        builder:AnnotatedString.Builder,
        start: Int,
        end: Int
    ) {
        var color = color
        if ("css" == language && isOfType("string", type, alias)) {
            super.applyColor(language, type, alias, -0x6591c6, builder, start, end)
            builder.addStyle(
                SpanStyle(background = Color(-0x7f000001)),
                start = start,
                end = end
            )
//            builder.setSpan(
//                BackgroundColorSpan(-0x7f000001),
//                start,
//                end,
//                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
//            )
            return
        }
        if (isOfType("namespace", type, alias)) {
            color = applyAlpha(.7f, color)
        }
        super.applyColor(language, type, alias, color, builder, start, end)
        if (isOfType("important", type, alias)
            || isOfType("bold", type, alias)
        ) {
            builder.addStyle(
                SpanStyle(fontWeight = FontWeight.Bold, fontStyle = FontStyle.Italic),
                start = start,
                end = end
            )
            builder.strongEmphasis(start, end)
        }
        if (isOfType("italic", type, alias)) {
            builder.emphasis(start, end)
        }
    }

    companion object {
        fun create(): Prism4jThemeObsidian {
            return Prism4jThemeObsidian(0xff000000)
        }

        /**
         * @since 3.0.0
         */
        fun create(@ColorInt background: Long): Prism4jThemeObsidian {
            return Prism4jThemeObsidian(background)
        }
    }
}